home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AmigActive 10
/
AACD 10.iso
/
AACD
/
Magazine
/
Online
/
httpproxy
/
src
/
httpresolve.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-08-20
|
5KB
|
243 lines
/*(( "Header" */
/*
* $Id: httpresolve.c,v 1.4 1996/08/12 03:33:36 mshopf Exp mshopf $
*
* (c) 1995-96 Matthias Hopf
*
* A cache file name resolver for HttpProxy.
*/
/*
* $Log: httpresolve.c,v $
* Revision 1.4 1996/08/12 03:33:36 mshopf
* SAVE option added. possiblity to write stdin into cache file.
*
* Revision 1.3 1996/08/11 22:25:15 mshopf
* reworked debug messages.
*
* Revision 1.2 1996/07/30 13:57:03 mshopf
* new version switch.
* standard version string added.
*
* Revision 1.1 1996/07/17 16:42:42 mshopf
* Initial revision
*
*/
/*)) */
/*(( "Includes & Konstanten" */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include "httpproxy.h"
#include "cache.h"
#include "httpproxy_rev.h"
#define MAX_COPYBUF (50 * 1024)
/*)) */
/*(( "Global Variables" */
long __oslibversion = 37;
const char *Version = "\0$VER: httpresolve 1.0 " __AMIGADATE__ "cache " CACHE_VERSION;
#ifdef DEBUG
int DebugLevel = -1;
#endif
static FILE *OutStream = stdout;
static struct RDArgs *RDArgs = NULL;
static char *Template = "URL/K,CACHE=FILE/K,TO/K,VERSION=VER/S,SAVE=WRITE/S";
#define arg_url ((char *) (RetArray[0]))
#define arg_file ((char *) (RetArray[1]))
#define arg_to ((char *) (RetArray[2]))
#define arg_ver ((long) (RetArray[3]))
#define arg_save ((long) (RetArray[4]))
/*)) */
/*(( "ExitAll () / ExitErr ()" */
/* Close everything and return */
void ExitAll (int Ret)
{
if (OutStream && OutStream != stdout)
fclose (OutStream);
OutStream = NULL;
if (RDArgs)
FreeArgs (RDArgs);
RDArgs = NULL;
exit (Ret);
}
void ExitErr (const char *Msg)
{
fprintf (stderr, "%s\n", Msg);
ExitAll (10);
}
/*)) */
/*(( "CopyInCache ()" */
/* Copy FILE to Cache */
void CopyInCache (FILE *in, cachefile_t *Cache, const char *Url)
{
char *Data;
int num;
if (CacheOpenNew (Cache, Url))
ExitErr ("cachefile open failed");
if (! (Data = AllocMem (MAX_COPYBUF, 0)) )
{
CacheClose (Cache, FALSE);
ExitErr ("not enough memory for copy operation");
}
while ( (num = fread (Data, 1, MAX_COPYBUF, in)) > 0)
CacheWrite (Cache, Data, num);
CacheClose (Cache, TRUE);
FreeMem (Data, MAX_COPYBUF);
}
/*)) */
/*(( "Action ()" */
/* Interprete Arguments */
/* RetArray has to be exactly written like this in order to have the arg_* macros working */
void Action (long *RetArray)
{
static cachefile_t Cache;
char Buffer [MAX_URLBUFFER];
if (arg_url && arg_file)
ExitErr ("don't specify both URL and CACHE=FILE");
if (! (arg_url || arg_file))
ExitErr ("please specify exactly one of URL and CACHE=FILE");
if (arg_save && ! arg_url)
ExitErr ("only use SAVE=WRITE together with URL");
/* - destination file */
if (arg_to)
if (! (OutStream = fopen (arg_to, "wb+")))
ExitErr ("can't open file");
if (arg_url)
{
if (! CacheInit ())
ExitErr ("internal error");
if (CacheGet (&Cache, arg_url, NULL) == -1)
ExitErr ("resolve error");
fprintf (OutStream, Cache.FileName);
if (arg_save)
CopyInCache (stdin, &Cache, arg_url);
CacheClose (&Cache, TRUE);
CacheExit ();
}
else
{
if (! CacheResolve (arg_file, Buffer))
ExitErr ("resolve error");
fprintf (OutStream, Buffer);
}
}
/*)) */
/*(( "main ()" */
/* main function */
void main (int argc, char *argv[])
{
long RetArray[] = { 0, 0, 0, 0, 0 };
BPTR lock;
/* Get arguments */
if (! (RDArgs = ReadArgs (Template, RetArray, NULL)))
{
fprintf (stderr, "%s\n"
"\tURL: Convert URL to cache filename.\n"
"\tCACHE: Convert cache filename to URL.\n"
"\tTO: Write output on success into this file.\n"
"\tVERSION: Output cache version number.\n"
"\tSAVE: Write stdin to cache file.\n",
Template);
ExitAll (10);
}
if (arg_ver)
{
fprintf (stdout, CACHE_VERSION "\n");
ExitAll (0);
}
if (! (lock = Lock (CACHEDIRVALIDFILE, ACCESS_READ)) )
ExitErr ("please invoke only inside cache directory");
UnLock (lock);
Action (RetArray);
ExitAll (0);
}
/*)) */
/*(( "assert()" */
#ifndef NDEBUG
void ASSERT (int x, const char *text, const char *file, const char *func, int line)
{
if (x)
return;
if (! text)
text = "unknown";
fprintf (stderr, "assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line);
ExitAll (20);
/*NOTREACHED*/
}
#endif /* NDEBUG */
/*)) */
/*(( "LogErr ()" */
/* We use our own (smaller and less mighty) LogErr */
void LogErr (request_t *Req, const char *Status, const char *Url, int ErrNo, const char *Reason, ...)
{
va_list Args;
char IoErrTxt [128];
va_start (Args, Reason);
fprintf (stderr, "Error ");
vfprintf (stderr, Reason, Args);
if (ErrNo < 0)
{
Fault (IoErr (), "", IoErrTxt, 128);
fprintf (stderr, "%s\n", IoErrTxt);
}
else
fprintf (stderr, "\n");
va_end (Args);
}
/*)) */